/**
 * Created by samisen on 17/7/25.
 */

$(function () {

    // 选项卡选中
    // var ss= $("li #at1").tab("show");
    SetNavScroll();
    $('[data-toggle="tooltip"]').tooltip();

    var $newTitle = $(".news-title");
    $("#news .nav-pills a").on("click", function () {
        var $this=$(this);
        $newTitle.text($this.data("title"));
    });

    //滚动条手指滑动移动

    var startX,endX;
    $carousels=$(".carousel");

    $carousels.on("touchmove",function (ele) {
       var moveX= ele.originalEvent.touches[0].clientX;
        if(startX==undefined){
            startX=moveX;
        }else {
            endX=moveX;
        }
    });

    $carousels.on("touchend",function (ele) {
        $(this).carousel(endX-startX>0?'prev':'next');
        startX=undefined;
    });

    $(window).resize(SetNavScroll);
    $(window).resize(changeADImage).trigger(changeADImage());
});

function SetNavScroll() {
    var $navTabs = $('.nav-tabs');
    var width = 30;

    $navTabs.children().each(function (index, ele) {
        width += $(ele).width();
    });

    if ($(window).width() < width) {
        $navTabs.css("width", width).parent().css("overflow-x", "scroll");
    }
}

function changeADImage() {
    var windowWidth = $(window).width();
    $(".carousel-inner .ad-carousel").each(function (index, item) {
        var imageSrc = windowWidth > 768 ? $(item).data("image-lg") : $(item).data("image-sm");
        if (windowWidth > 768) {
            $(item).css("backgroundImage", "url(" + imageSrc + ")");
            $(item).html("");
        } else {
            $(item).html('<img src="' + imageSrc + '" alt="">');
            $(item).css("backgroundImage", "");
        }
    });
}

